这是我在这个网站上的第一篇文章,所以我会做空!
我是Android Studio中的新手,并尝试制作应用..问题出现在这里: 我创建了一个代码按钮,以便在按下时将其重定向到新活动。这项工作在模拟器上很好。然后在那个新的活动中我创建了另一个按钮,重定向到另一个acrivity并以与前一个按钮相同的方式对其进行编码(ofc我更改名称和所有这些)但是当在模拟器上运行app时,第一个按钮工作得很好然后我按下第二个按钮。它给了我一个应用程序强制关闭错误..我不明白,为什么,Android Studio不报告代码中的任何错误,但新按钮不起作用。
我使用本教程创建了这些按钮:http://developer.android.com/training/basics/firstapp/starting-activity.html
任何人都知道解决方案吗?
content_my.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.dario.mojaaplikacija.MyActivity"
tools:showIn="@layout/activity_my">
<Button
android:id="@+id/button_vjezbe"
android:text="@string/button_vjezbe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="vjezbe" />
</LinearLayout>
MyActivity.java
public class MyActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE ="com.example.dario.mojaaplikacija";
public void vjezbe(View view){
Intent intent = new Intent(this, BenchPress.class);
// String varijabla = editText.getText().toString();
//intent.putExtra(EXTRA_MESSAGE, varijabla);
startActivity(intent);
content_bench_press.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.dario.mojaaplikacija.BenchPress"
tools:showIn="@layout/activity_bench_press">
<Button
android:text="@string/button_o_benchu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="benchA"/>
</LinearLayout>
BenchPress.java
public class BenchPress extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.example.dario.mojaaplikacija";
public void benchA (View view){
Intent intent = new Intent(this, BenchA.class);
startActivity(intent);
content_bench.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/conten"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.dario.mojaaplikacija.BenchA"
tools:showIn="@layout/activity_bencha">
</RelativeLayout>
BenchA.java
Intent intent = getIntent();
String message = intent.getStringExtra(BenchPress.EXTRA_MESSAGE);
strings.xml
<resources>
<string name="app_name">MojaAplikacija</string>
<string name="action_settings">Settings</string>
<string name="button_vjezbe">Vježbe</string>
<string name="title_activity_display_message">MyMessage</string>
<string name="title_activity_bench_press">BenchPress</string>
<string name="button_o_benchu">Bench</string>
<string name="title_activity_bench">BenchA</string>
</resources>
点击第二个按钮时得到的RED代码:
01-09 22:27:23.091 2431-2431/com.example.dario.mojaaplikacija E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dario.mojaaplikacija, PID: 2431
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.dario.mojaaplikacija/com.example.dario.mojaaplikacija.BenchA}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at com.example.dario.mojaaplikacija.BenchA.<init>(BenchA.java:30)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
BenchA.java
package com.example.dario.mojaaplikacija;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class BenchA extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bencha);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
Intent intent = getIntent();
String message = intent.getStringExtra(BenchPress.EXTRA_MESSAGE)
答案 0 :(得分:0)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at com.example.dario.mojaaplikacija.BenchA.<init>(BenchA.java:30)
这告诉您检查BenchA中的第30行
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.dario.mojaaplikacija/com.example.dario.mojaaplikacija.BenchA}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
这告诉您有一个空指针异常。您可以在stackoverflow上阅读更多内容,但它基本上意味着您尝试在intent上调用getStringExtra,但intent对象为null。
Intent intent = getIntent();
返回null。它无法找到它。这就是后续线路崩溃的原因。 在if (intent != null)
中换行以下行以避免NullPointerException
要解决这个问题,我不想建议你离教程太远,但有一个选择是传入一个Bundle,在你的新活动的onCreate中,你可以得到这个包
另一件事:在BenchPress类中,你从未传递任何String附加功能。所以你试图获得一个从未意图开始的字符串。字符串消息将返回null;
编辑:既然你已经发布了你的类,看起来getIntent作为ActivityClass的一个属性浮动。当然,Java是可以的,因为你只是在这两行中定义它。但是,如果您要编写指令,例如,打印出您的消息或显示文本,则无法编译。将两行Intent intent...
和String message...
放入您的onCreate。 我坚信这就是getIntent()返回null的原因。该类无法访问getIntent(),但是当您覆盖oncreate方法时,您可以访问getIntent()
编辑:
以下是您应该做的事情,这是您打算抓住的内容:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bencha);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent = getIntent();
String message = intent.getStringExtra(BenchPress.EXTRA_MESSAGE)
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
答案 1 :(得分:0)
制作以下两种方法:
第一个按钮的onClick方法:
public void onClickFirstButton(View view){
//your code go here...
Intent intent = new Intent(this, the_name_class_of_new_activity.class);
startActivity(intent);//you can start the new activity at any time with this line
}
对于第二个按钮是相同的方法(更改方法的名称和 把你的第二个新活动的名称。
public void onClickSecondButton(View view){
//your code go here...
Intent intent = new Intent(this, the_name_class_of_second_new_activity.class);
startActivity(intent);//you can start the new activity at any time with this line
}
答案 2 :(得分:0)
我简直不敢相信! 我解决了!!
我的BenchA.java现在看起来像这样:
package com.example.dario.mojaaplikacija;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class BenchA extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bencha);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
Intent intent = getIntent();
if (intent !=null) {
String message = intent.getStringExtra(BenchPress.EXTRA_MESSAGE);
} }
});
}
}
刚刚推断出if类中的语句和按钮有效!
仍然有一些红色代码:
01-10 00:03:56.469 2567-2584/com.example.dario.mojaaplikacija E/Surface: getSlotFromBufferLocked: unknown buffer: 0xabef7ee0
01-10 00:03:58.755 2567-2584/com.example.dario.mojaaplikacija E/Surface: getSlotFromBufferLocked: unknown buffer: 0xabef9290
不知道这意味着什么,但主要的是它正在工作!!!!