开发者

Android: Activity to Activity, 2nd activity cannot call setContentView?

I have activity 1 (A1) going into activity 2 (A2) and am getting crashes at the setContentView in A2. A1 is a ListActivity that pulls the list from xml and uses an Intent to pass data to be displayed in A2 (putExtra). A2 pulls in xml and uses the Extra data from A1 to know what to display, but my setContentView line crashes the app.

Looking into possible solutions I've read that setContentView must be called from a UI thread; is my A2 not a UI thread?

Code:

-- A1 --

public class activity extends ListActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    //setContentView(R.layout.main);
    String[] rmenu = getResources().getStringArray(R.array.root_menu);
    if (rmenu != null) {
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, rmenu));
    }
}
@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // Get the item that was clicked
        //Object o = this.getListAdapter().getItem(position);
        //String keyword = o.toString();
        //Toast.makeText(this, "You selected: " + position,     Toast.LENGTH_LONG).show();
        //
        Intent myIntent = new Intent(v.getContext(), Activity1.class);
    myIntent.putExtra("com.activity.Key", position);
    startActivity(myIntent);
    }
}

-- A2 --

public class Activity1 extends activity {

/** Called when the activity is first created. */
    @Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    //setContentVie开发者_运维问答w(R.layout.main2);
    int data = 0;
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        data = extras.getInt("com.activity.Key");
    } else {
        data = 0;
    }
    String[] con = getResources().getStringArray(R.array.content);
    //Toast.makeText(context, con[data], Toast.LENGTH_SHORT).show();
    TextView tv = new TextView(this);
    String dis = con[data];
    tv.setText(dis);
    setContentView(tv);
}
}

I've read the docs on Activities and ContentViews but they don't do the best job of explaining what cannot be done. Am I tripping over my own feet here?

Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.activity"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".activity" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
    <activity android:name=".Activity1" android:label="@string/app_name">    </activity>
</application>
</manifest>


Your class in A2 extends the class in A1 (activity). You probably want to extend android.app.Activity.


You can only call setContentView with a Layout resource (e.g. R.layout.mylayout), not a View object like the TextView you have used here.


The answer is given by the inheritance.

The flow for A1: activity extends ListActivity and calls setContentView(), that ok.

The flow for A2: Activity1 extends activity. onCreate() of Activity1 calls super.onCreate() which is the activity.onCreate(). There you already have called setContentView() so you can't call it in Activity1.onCreate() again.


Your codes a bit all over the place but you want something a bit more like this. You biggest problem is you actually had commented the line you needed! setContentView(R.layout.main);

  public class Activity1 extends activity {

    private EditText mText;

    /** Called when the activity is first created. */
        @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            data = extras.getInt("com.activity.Key");
        } else {
            data = 0;
        }
        mText = (EditText) findViewById(R.id.your_text_field);
        mText.setText(Integer.toString(data));
    }
  }

I hope that helps

i suggest you do the Google notepad tutorials, especially notepad 2 it does what you need

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜