Unable to instantiate activity Component, ClassCastExceptio我是新來的android,所以我跟着不同的教程來完成我的任務。我有一個錯誤,儘管所有的努力仍然無法解決。我有一個主要活動,我稱之爲積極主動(如果用戶在提醒時選擇「是」)。積極的活動基本上包含三個項目的列表視圖,我想顯示用戶點擊第一個項目時的移動聯繫人。 我把聯繫人的代碼檢索到的片段類,但它給出了即使在我沒有使用簡單的意圖調用片段的鑄造錯誤。 爲主要活動的代碼如下:ClassCastException(片段不能轉換爲android.app.Activity)
public class activity_home extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Context context = this;
new AlertDialog.Builder(context)
.setTitle("Alert")
.setMessage("Do you want to enable the app right now?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int id) {
// go to a new activity of the app
Intent positiveActivity = new
Intent(getApplicationContext(),
PositiveActivity.class);
startActivity(positiveActivity);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setNeutralButton("Remind Me later", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int
which) {
// do nothing
}
}
)
.
setIcon(android.R.drawable.ic_dialog_alert)
.show();
}}
PositiveActivity.java:
public class PositiveActivity extends FragmentActivity {
ListView listView;
ArrayList<String> listname;
ArrayList<String> list_no;
Context context;
LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.positive_view);
Context context = this;
listView = (ListView) findViewById(R.id.mobile_list);
TextView textView = new TextView(context);
textView.setTypeface(null, Typeface.BOLD);
textView.setHeight(100);
textView.setText("Define Specifications for Enabling Auto Pickup!");
listView.addHeaderView(textView);
// Array of strings...
String[] mobileArray = new String[] {"Add Numbers to the list","Select
Timer","Pick Selection Mode"};
final ArrayAdapter <String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
mobileArray);
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String)
listView.getItemAtPosition(position);
if (itemPosition == 1) {
ContactListFragment fragmentS1 = new ContactListFragment();
getSupportFragmentManager().beginTransaction().replace
(R.id.headlines_fragment,
fragmentS1).commit();
}}});}}
And the code of Fragment:
import android.support.v4.widget.SimpleCursorAdapter;
public class ContactListFragment extends ListFragment implements
LoaderCallbacks<Cursor> {
private CursorAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// create adapter once
Context context = getActivity();
int layout = android.R.layout.simple_list_item_1;
Cursor c = null; // there is no cursor yet
int flags = 0; // no auto-requery! Loader requeries.
mAdapter = new SimpleCursorAdapter(context, layout, c, FROM, TO, flags);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// each time we are started use our listadapter
setListAdapter(mAdapter);
// and tell loader manager to start loading
getLoaderManager().initLoader(0, null, this);
}
// columns requested from the database
private static final String[] PROJECTION = {
Contacts._ID, // _ID is always required
Contacts.DISPLAY_NAME_PRIMARY // that's what we want to display
};
// and name should be displayed in the text1 textview in item layout
private static final String[] FROM = { Contacts.DISPLAY_NAME_PRIMARY };
private static final int[] TO = { android.R.id.text1 };
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// load from the "Contacts table"
Uri contentUri = Contacts.CONTENT_URI;
// no sub-selection, no sort order, simply every row
// projection says we want just the _id and the name column
return new CursorLoader(getActivity(),
contentUri,
PROJECTION,
null,
null,
null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Once cursor is loaded, give it to adapter
mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// on reset take any old cursor away
mAdapter.swapCursor(null);
}
}
如果有人指出,在代碼中的錯誤,我會感激
發佈錯誤/ logcat的/ stacktrace將得到更好的答案更快 – buradd
@buradd我編輯了問題,並添加圖片到錯誤logcat顯示。 – LearningNeverEnds