I want to take a picture from file chooser and show it on an image view.. but when I run my app it would be stopped and doesn't work. where is problem? this codes are code of 'MainActivity.java'.I didn't change any thing in other files of my project.MY codes are based on this link .My codes are:
public class MainActivity extends Activity {
private static final int FILE_SELECT_CODE = 0;
private static final String TAG = null;
public TextView tv=(TextView) findViewById(R.id.textView1);
ImageView iv=(ImageView) findViewById(R.id.imageView1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showFileChooser();
}
});
}
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
iv.setImageURI(uri);
Log.d(TAG, "File Uri: " + uri.toString());
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
log is:
12-12 04:21:32.343: D/AndroidRuntime(1068): Shutting down VM
12-12 04:21:32.343: W/dalvikvm(1068): threadid=1: thread exiting with uncaught exception (group=0xb4a60b90)
12-12 04:21:32.433: E/AndroidRuntime(1068): FATAL EXCEPTION: main
12-12 04:21:32.433: E/AndroidRuntime(1068): Process: com.example.a55j, PID: 1068
12-12 04:21:32.433: E/AndroidRuntime(1068): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.a55j/com.example.a55j.MainActivity}: java.lang.NullPointerException
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.os.Handler.dispatchMessage(Handler.java:102)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.os.Looper.loop(Looper.java:137)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-12 04:21:32.433: E/AndroidRuntime(1068): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 04:21:32.433: E/AndroidRuntime(1068): at java.lang.reflect.Method.invoke(Method.java:515)
12-12 04:21:32.433: E/AndroidRuntime(1068): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-12 04:21:32.433: E/AndroidRuntime(1068): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-12 04:21:32.433: E/AndroidRuntime(1068): at dalvik.system.NativeStart.main(Native Method)
12-12 04:21:32.433: E/AndroidRuntime(1068): Caused by: java.lang.NullPointerException
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.Activity.findViewById(Activity.java:1883)
12-12 04:21:32.433: E/AndroidRuntime(1068): at com.example.a55j.MainActivity.<init>(MainActivity.java:23)
12-12 04:21:32.433: E/AndroidRuntime(1068): at java.lang.Class.newInstanceImpl(Native Method)
12-12 04:21:32.433: E/AndroidRuntime(1068): at java.lang.Class.newInstance(Class.java:1208)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
12-12 04:21:32.433: E/AndroidRuntime(1068): ... 11 more
12-12 04:21:34.823: I/Process(1068): Sending signal. PID: 1068 SIG: 9
activiyt_main xml is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginLeft="16dp"
android:layout_marginTop="35dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/open" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginTop="81dp"
android:layout_toLeftOf="@+id/button1"
android:src="@drawable/ic_launcher" />
</RelativeLayout>