ClassCastException fragments.MainActivity must implement OnHeadlineSelectedListener
Problem:
You have two class with similar names HeadLinesFragment and HeadMyLinesFragment with OnHeadlineSelectedListener.
Check if call correct class in MainActivity.
For example if use HeadMyLinesFragment change implement to HeadMyLinesFragment too!
You have two class with similar names HeadLinesFragment and HeadMyLinesFragment with OnHeadlineSelectedListener.
Check if call correct class in MainActivity.
For example if use HeadMyLinesFragment change implement to HeadMyLinesFragment too!
public class MainActivity extends FragmentActivity
implements HeadLinesFragment.OnHeadlineSelectedListener {
// wrong implements you need correct class name
//implements HeadMyLinesFragment.OnHeadlineSelectedListener
//............
HeadMyLinesFragment firstFragment = new HeadMyLinesFragment(); // because in code using HeadMyLinesFragment
396LW NO topic_id
AD
Další témata ....(Topics)
How get application version, sdk version, package name defined in the AndroidManifest file programmically Android sample.
MainClass.java onCreate()
AndroidManifes.xml
MainClass.java onCreate()
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// in onCreate
PackageInfo pinfo = this.getPackageManager().getPackageInfo(getPackageName(), 0);
String sVersionCode = pinfo.versionCode; // 1
String sVersionName = pinfo.versionName; // 1.0
String sPackName = getPackageName(); // cz.okhelp.my_app
int nSdkVersion = Integer.parseInt(Build.VERSION.SDK); // 7
int nSdkVers = Build.VERSION.SDK_INT; // 7
}
AndroidManifes.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="//schemas.android.com/apk/res/android"
package="cz.okhelp.my_app"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Add_view_to_tableActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Change table row background color if user click on row Android example code.
MainActivity.java
main.xml
strings.xml
MainActivity.java
public class MainActivity extends Activity {
Boolean bColorYellow = true;
TextView hTextView;
TableRow hTableRow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hTextView = (TextView)findViewById(R.id.idTextView);
hTableRow = (TableRow)findViewById(R.id.idTableRow1);
} // end onCreate
public void myTableRowClickHandler(View view) {
switch (view.getId()) {
case R.id.idTableRow1:{
if(bColorYellow){
hTableRow.setBackgroundColor(Color.GREEN);
bColorYellow = false;
}
else{
hTableRow.setBackgroundColor(Color.YELLOW);
bColorYellow = true;
}
}
break;
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/tableLayout1">
<TableRow android:id="@+id/idTableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#5655AA"
android:onClick="@string/myTableRowClick"
android:focusable="true">
<TextView
android:id="@+id/idTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</TableRow>
</TableLayout>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World!</string>
<string name="app_name">TableRow</string>
<string name="myTableRowClick">myTableRowClickHandler</string>
</resources>
Adding TableRow with a View TextView (EditText Button) programmically dynamically to TableLayout Android sample basic example.
MainClass.java
main.xml
MainClass.java
public class MainClass extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow row = new TableRow(this);
TextView tv = new TextView(this);
tv.setText("This is text");
tl.addView(row);
row.addView(tv);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableLayout android:layout_width="fill_parent"
android:id="@+id/tableLayout1" android:layout_height="wrap_content">
</TableLayout>
</LinearLayout>
You can get list by class Build
For example:
List of Build class getting from emulator:
"BOARD=unknown
BRAND=generic
CPU_ABI=armeabi
DEVICE=generic
DISPLAY=sdk-eng 2.1-update1 ECLAIR 35983 test-keys
FINGERPRINT=generic/sdk/generic/:2.1-update1/ECLAIR/35983:eng/test-keys
HOST=android-test-13.mtv.corp.google.com
ID=ECLAIR
MANUFACTURER=unknown
MODEL=sdk
PRODUCT=sdk
TAGS=test-keys
TIME=1273161972000
TYPE=eng
USER=android-build
"
For example:
if(Build.MANUFACTURER.equals("unknown")) {
// Emulator
}
List of Build class getting from emulator:
"BOARD=unknown
BRAND=generic
CPU_ABI=armeabi
DEVICE=generic
DISPLAY=sdk-eng 2.1-update1 ECLAIR 35983 test-keys
FINGERPRINT=generic/sdk/generic/:2.1-update1/ECLAIR/35983:eng/test-keys
HOST=android-test-13.mtv.corp.google.com
ID=ECLAIR
MANUFACTURER=unknown
MODEL=sdk
PRODUCT=sdk
TAGS=test-keys
TIME=1273161972000
TYPE=eng
USER=android-build
"
If you have to type frequently the same code you can create templates which can be activate via autocomplete with Ctrl + Space.
For example lets assume setOnClickListener body.
To create a template for this select the menu Window->Preferences and Open Java -> Editor -> Templates
[caption id="attachment_1114" align="alignleft" width="300" caption="Eclipse-show-template-proposals"][/caption]
[caption id="attachment_1116" align="alignleft" width="300" caption="Eclipse - select your template"][/caption]
Quick help in Eclipse editor: select keyword and press Ctrl+1
For example lets assume setOnClickListener body.
To create a template for this select the menu Window->Preferences and Open Java -> Editor -> Templates
Type name of template in Eclipse s editor and pres Ctrl+Space
[caption id="attachment_1114" align="alignleft" width="300" caption="Eclipse-show-template-proposals"][/caption]
Select your template from intellisense help and press Enter or double click on selected item.
[caption id="attachment_1116" align="alignleft" width="300" caption="Eclipse - select your template"][/caption]
mIdButtonHome.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("//android.okhelp.cz/category/software/"));
startActivity(browserIntent);
}
});
Quick help in Eclipse editor: select keyword and press Ctrl+1
Editace: 2016-04-01 12:06:43
Počet článků v kategorii: 396
Url:classcastexception-fragments-mainactivity-must-implement-onheadlineselectedlistener