I am a begginner trying to build android apps.I just copy pasted the code in the Eclipse software.But I dont know what is going wrong here.The application isnt getting opened.Hope my doubts will be cleared.Thanks in advance.
Here is my fragment_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/my_text"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text" />
Here is my MainActivity.java:
package com.MyButtonApp.mybuttonapp;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
Button myButton;
EditText myText;
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myButton=(Button)findViewById(R.id.button1);
myText=(EditText)findViewById(R.id.my_text);
myText.setSingleLine();
myText.setInputType(InputType.TYPE_NULL);
setContentView(R.layout.activity_main);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myButton.setText("You hav clicked"+ ++count+ "times.!!!");
}
});
Here is the logCat:
07-03 05:45:46.860: D/AndroidRuntime(991): Shutting down VM.
07-03 05:45:46.860: W/dalvikvm(991): threadid=1: thread exiting with uncaught exception
(group=0xb2a23ba8)07-03 05:45:46.880: E/AndroidRuntime(991): FATAL EXCEPTION:main
07-03 05:45:46.880: E/AndroidRuntime(991): Process: com.MyButtonApp.mybuttonapp,PID:991
07-03 05:45:46.880: E/AndroidRuntime(991): java.lang.RuntimeException:Unable to start
activityComponentInfo{com.MyButtonApp.mybuttonapp/com.MyButtonApp.mybuttonapp.
MainActivity}
:java.lang.NullPointerException
07-03 05:45:46.880: E/AndroidRuntime(991):at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-03 05:45:46.880: E/AndroidRuntime(991): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-03 05:45:46.880: E/AndroidRuntime(991): at
android.app.ActivityThread.access$800(ActivityThread.java:135)
07-03 05:45:46.880: E/AndroidRuntime(991): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-03 05:45:46.880: E/AndroidRuntime(991): at
android.os.Handler.dispatchMessage(Handler.java:102)
07-03 05:45:46.880: E/AndroidRuntime(991): at android.os.Looper.loop(Looper.java:136)
07-03 05:45:46.880: E/AndroidRuntime(991): at
android.app.ActivityThread.main(ActivityThread.java:5017)
07-03 05:45:46.880: E/AndroidRuntime(991): at
java.lang.reflect.Method.invokeNative(Native Method)
07-03 05:45:46.880: E/AndroidRuntime(991): at
java.lang.reflect.Method.invoke(Method.java:515)
07-03 05:45:46.880: E/AndroidRuntime(991): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-03 05:45:46.880: E/AndroidRuntime(991): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-03 05:45:46.880: E/AndroidRuntime(991): at dalvik.system.NativeStart.main(Native
Method)
07-03 05:45:46.880: E/AndroidRuntime(991): Caused by: java.lang.NullPointerException
07-03 05:45:46.880: E/AndroidRuntime(991): at
com.MyButtonApp.mybuttonapp.MainActivity.onCreate(MainActivity.java:28)
07-03 05:45:46.880: E/AndroidRuntime(991): at
android.app.Activity.performCreate(Activity.java:5231)
07-03 05:45:46.880: E/AndroidRuntime(991): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-03 05:45:46.880: E/AndroidRuntime(991): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-03 05:45:46.880: E/AndroidRuntime(991): ... 11 more
07-03 05:45:52.400: I/Process(991): Sending signal. PID: 991 SIG: 9
07-03 05:52:10.125: D/AndroidRuntime(1302): Shutting down VM
07-03 05:52:10.125: W/dalvikvm(1302): threadid=1: thread exiting with uncaught
exception (group=0xb2a23ba8)
07-03 05:52:10.135: E/AndroidRuntime(1302): FATAL EXCEPTION: main
07-03 05:52:10.135: E/AndroidRuntime(1302): Process: com.MyButtonApp.mybuttonapp, PID:
1302
07-03 05:52:10.135: E/AndroidRuntime(1302): java.lang.RuntimeException: Unable to start
activity
ComponentInfo{com.MyButtonApp.mybuttonapp/com.MyButtonApp.mybuttonapp.MainActivity}:
java.lang.NullPointerException
07-03 05:52:10.135: E/AndroidRuntime(1302): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-03 05:52:10.135: E/AndroidRuntime(1302): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-03 05:52:10.135: E/AndroidRuntime(1302): at
android.app.ActivityThread.access$800(ActivityThread.java:135)
07-03 05:52:10.135: E/AndroidRuntime(1302): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-03 05:52:10.135: E/AndroidRuntime(1302): at
android.os.Handler.dispatchMessage(Handler.java:102)
07-03 05:52:10.135: E/AndroidRuntime(1302): at
android.os.Looper.loop(Looper.java:136)
07-03 05:52:10.135: E/AndroidRuntime(1302): at
android.app.ActivityThread.main(ActivityThread.java:5017)
07-03 05:52:10.135: E/AndroidRuntime(1302): at
java.lang.reflect.Method.invokeNative(Native Method)
07-03 05:52:10.135: E/AndroidRuntime(1302): at
java.lang.reflect.Method.invoke(Method.java:515)
07-03 05:52:10.135: E/AndroidRuntime(1302): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-03 05:52:10.135: E/AndroidRuntime(1302): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-03 05:52:10.135: E/AndroidRuntime(1302): at
dalvik.system.NativeStart.main(Native Method)
07-03 05:52:10.135: E/AndroidRuntime(1302): Caused by: java.lang.NullPointerException
07-03 05:52:10.135: E/AndroidRuntime(1302): at
com.MyButtonApp.mybuttonapp.MainActivity.onCreate(MainActivity.java:28)
07-03 05:52:10.135: E/AndroidRuntime(1302): at
android.app.Activity.performCreate(Activity.java:5231)
07-03 05:52:10.135: E/AndroidRuntime(1302): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-03 05:52:10.135: E/AndroidRuntime(1302): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-03 05:52:10.135: E/AndroidRuntime(1302): ... 11 more
Here is AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.MyButtonApp.mybuttonapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.MyButtonApp.mybuttonapp.MainActivity"
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>
You should change the order like
setContentView(R.layout.activity_main);
myButton=(Button)findViewById(R.id.button1);
myText=(EditText)findViewById(R.id.my_text);
First you need to setContentView(R.layout.activity_main);
and then reference your View.
I think you are initialising wrong layout.Change setContentView(R.layout.activity_main); to setContentView(R.layout.fragment_main);
then put setContentView(R.layout.fragment_main) before findViewById(); like:
setContentView(R.layout.fragment_main);
myButton=(Button)findViewById(R.id.button1);
myText=(EditText)findViewById(R.id.my_text);
Move the findViewById()
to the fragment onCreateView()
or onViewCreated()
after you've inflated the fragment_main layout you posted.
The views you're attempting to find are in the fragment layout and activity onCreate()
is too early to find them. (Finding activity views before setContentView()
would be a problem in case the views were in your activity layout.)
See NullPointerException accessing views in onCreate() for more.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.