我是Android开发新手,我在创建新Activity
方面遇到了一些问题。我想在点击Activity
但是应用崩溃时开始新的Button
。以下是控制台和logcat中的内容:
logcat的
03-11 00:21:08.639: D/AndroidRuntime(336): Shutting down VM
03-11 00:21:08.649: W/dalvikvm(336): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-11 00:21:08.659: E/AndroidRuntime(336): FATAL EXCEPTION: main
03-11 00:21:08.659: E/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.src.vicnote/com.src.vicnote.MainActivity}: java.lang.NullPointerException
03-11 00:21:08.659: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-11 00:21:08.659: E/AndroidRuntime(336): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-11 00:21:08.659: E/AndroidRuntime(336): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-11 00:21:08.659: E/AndroidRuntime(336): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-11 00:21:08.659: E/AndroidRuntime(336): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 00:21:08.659: E/AndroidRuntime(336): at android.os.Looper.loop(Looper.java:123)
03-11 00:21:08.659: E/AndroidRuntime(336): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-11 00:21:08.659: E/AndroidRuntime(336): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 00:21:08.659: E/AndroidRuntime(336): at java.lang.reflect.Method.invoke(Method.java:507)
03-11 00:21:08.659: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-11 00:21:08.659: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-11 00:21:08.659: E/AndroidRuntime(336): at dalvik.system.NativeStart.main(Native Method)
03-11 00:21:08.659: E/AndroidRuntime(336): Caused by: java.lang.NullPointerException
03-11 00:21:08.659: E/AndroidRuntime(336): at com.src.vicnote.MainActivity.onCreate(MainActivity.java:26)
03-11 00:21:08.659: E/AndroidRuntime(336): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-11 00:21:08.659: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-11 00:21:08.659: E/AndroidRuntime(336): ... 11 more
03-11 00:21:38.579: D/AndroidRuntime(365): Shutting down VM
03-11 00:21:38.579: W/dalvikvm(365): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-11 00:21:38.599: E/AndroidRuntime(365): FATAL EXCEPTION: main
03-11 00:21:38.599: E/AndroidRuntime(365): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.src.vicnote/com.src.vicnote.MainActivity}: java.lang.NullPointerException
03-11 00:21:38.599: E/AndroidRuntime(365): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-11 00:21:38.599: E/AndroidRuntime(365): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-11 00:21:38.599: E/AndroidRuntime(365): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-11 00:21:38.599: E/AndroidRuntime(365): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-11 00:21:38.599: E/AndroidRuntime(365): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 00:21:38.599: E/AndroidRuntime(365): at android.os.Looper.loop(Looper.java:123)
03-11 00:21:38.599: E/AndroidRuntime(365): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-11 00:21:38.599: E/AndroidRuntime(365): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 00:21:38.599: E/AndroidRuntime(365): at java.lang.reflect.Method.invoke(Method.java:507)
03-11 00:21:38.599: E/AndroidRuntime(365): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-11 00:21:38.599: E/AndroidRuntime(365): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-11 00:21:38.599: E/AndroidRuntime(365): at dalvik.system.NativeStart.main(Native Method)
03-11 00:21:38.599: E/AndroidRuntime(365): Caused by: java.lang.NullPointerException
03-11 00:21:38.599: E/AndroidRuntime(365): at com.src.vicnote.MainActivity.onCreate(MainActivity.java:26)
03-11 00:21:38.599: E/AndroidRuntime(365): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-11 00:21:38.599: E/AndroidRuntime(365): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-11 00:21:38.599: E/AndroidRuntime(365): ... 11 more
activity_main
<pre><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.src.vicnote.MainActivity"
tools:ignore="MergeRootFrame" /><code>
fragment_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/MainScreen"
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="com.src.vicnote.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/buttonNew"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="New" />
</RelativeLayout>
Java代码
答案 0 :(得分:1)
从它看起来是你设置内容视图
setContentView(R.layout.activity_main);
但是您提供的内容中的activity_main.xml中没有按钮,它显示的是nullpointerexception,因为
newButton = (Button) findViewById(R.id.buttonNew);
为null,因为您提供的信息中的实际按钮位于
中fragment_main.xml
所以你可以试试
setContentView(R.layout.fragment_main);
答案 1 :(得分:1)
检查您的Manifest.xml 并确保你有一些
之王<activity android:name=".Mainfragment" />
答案 2 :(得分:0)
可能是xml中缺少声明
最有可能
R.id.buttonNew
澄清一下:您的活动正在寻找名为buttonNew但未找到它的按钮
答案 3 :(得分:0)
here you have to mention your button in fragment_main.xml and you have to set
在setContentView()方法中的activity_main.xml然后如何才能找到你的 按钮只是更改xml文件名。
如下所示:
的setContentView(R.layout.fragment_main);
答案 4 :(得分:0)
<li>Producer:
{% for producer in producers %}
{% if not forloop.last %}
{{ producer }},
{% else %}
{{ producer }}
{% endif %}
{% endfor %}
</li>
答案 5 :(得分:0)
package com.mvvmmodelrecyclerview;
import android.app.Activity;
import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.content.Intent;
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.widget.Button;
import android.os.Build;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//FragmentManager manager = getSupportFragmentManager();
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
Button newButton;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
newButton = (Button) rootView.findViewById(R.id.buttonNew);
newButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent newNote = new Intent(getActivity(), NewNoteActivity.class);
startActivity(newNote);
}
});
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/MainScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<FrameLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.src.vicnote.MainActivity"
tools:ignore="MergeRootFrame" />
</LinearLayout>
fragment_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/MainScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_gravity="center"
android:id="@+id/buttonNew"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="New" />
<TextView
android:gravity="center"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Fragmnt View" />
</LinearLayout>
答案 6 :(得分:0)
Or if you want to add button in activity_main.xml
just as like your code did...then try to do it proper way...like that...
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/MainScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_gravity="center"
android:id="@+id/buttonNew"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="New" />
<FrameLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.src.vicnote.MainActivity"
tools:ignore="MergeRootFrame" />
</LinearLayout>
fragment_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/MainScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:gravity="center_vertical"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Fragmnt View" />
</LinearLayout>
MainActivity.java
package com.mvvmmodelrecyclerview;
import android.app.Activity;
import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.content.Intent;
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.widget.Button;
import android.os.Build;
public class MainActivity extends Activity {
Button newButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newButton = (Button) findViewById(R.id.buttonNew);
newButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent newNote = new Intent(MainActivity.this, NewNoteActivity.class);
startActivity(newNote);
}
});
//FragmentManager manager = getSupportFragmentManager();
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
}