Reputation: 438
I am trying to do an intent from ShoutActivity that extends activity to MyListActivity that extends ListActivity on a Click
the App reaches when its reaches the intent
code:
first activity:
public class ShoutActivity extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
private Button b1;
private Button b2;
private EditText text_box;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button) this.findViewById(R.id.button1);
b1.setOnClickListener(this);
b2 = (Button) this.findViewById(R.id.button04);
b2.setOnClickListener(this);
//.....other stuff
}
public void onClick(View v) {
if (v==b1)
{
//not important
}
if(v==b2){
Intent intent = new Intent();
intent.setClass(ShoutActivity.this,MyListActivity.class);
);
Bundle myBundle = new Bundle();
myBundle.putStringArray("List", s1);
intent.putExtras(myBundle);
startActivity(intent);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<EditText android:layout_width="match_parent" android:id="@+id/editText1" android:layout_height="wrap_content">
<requestFocus></requestFocus>
</EditText>
<Button android:text="Shout !" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="View Shouts" android:id="@+id/button04" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="karim.mobi"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ShoutActivity"
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=".MyListActivity" android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar"/>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
</manifest>
second activity
package karim.mobi;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;
import android.widget.*;
import android.app.*;
public class MyListActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String s[];
s= savedInstanceState.getStringArray("List");
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s));
getListView().setTextFilterEnabled(true);
}
}
error logcat:
04-21 21:29:31.177: ERROR/AndroidRuntime(789): FATAL EXCEPTION: main
04-21 21:29:31.177: ERROR/AndroidRuntime(789): java.lang.RuntimeException: Unable to start activity ComponentInfo{karim.mobi/karim.mobi.MyListActivity}: java.lang.NullPointerException
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.os.Looper.loop(Looper.java:123)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at java.lang.reflect.Method.invoke(Method.java:521)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at dalvik.system.NativeStart.main(Native Method)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): Caused by: java.lang.NullPointerException
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at karim.mobi.MyListActivity.onCreate(MyListActivity.java:21)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
can you please tell me whats the problem ?
please be specific
Upvotes: 0
Views: 731
Reputation: 68177
First, you must need to set content-view for your MyListActivity. So you need to call the following right after super.onCreate:
setContentView(R.layout.your_layout_name);
Second, you are reading the array wrong in that same activity.
Change this:
s= savedInstanceState.getStringArray("List");
To this:
s= getIntent().getExtras().getStringArray("List");
Upvotes: 1
Reputation: 132982
use this :
Resources res = getResources () ;
String [] s1 = res . getStringArray ( R . array . strs_array );
Intent intent = new Intent();
intent.setClass(ShoutActivity.this,MyListActivity.class);
);
Bundle myBundle = new Bundle();
myBundle.putStringArray("List", s1);
intent.putExtras(myBundle);
ShoutActivity.this.startActivity(intent);
and in activity MyListActivity :
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle callerBundle = getIntent().getExtras();
String s[];
if(callerBundle !=null)
{
s=callerBundle.getStringArray("List");
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s));
getListView().setTextFilterEnabled(true);
}
Upvotes: 1