gpt4 book ai didi

android-activity - android Activity 崩溃无缘无故?也许是因为setcontentview?

转载 作者:行者123 更新时间:2023-12-03 16:12:20 25 4
gpt4 key购买 nike

我在Android开发上遇到了问题(我是新手顺便说一句)..

我正在尝试完成本教程:http://developer.android.com/training/basics/firstapp/starting-activity.html,除了我的问题是第二项 Activity 系统地崩溃了,我不知道为什么..

当我注释以下行时:setContentView(textView1):第二个 Activity 不会崩溃,但未显示文本。所以我想那是我的问题所在,但是我一直在找几个小时,但我似乎无法弄清楚:-s

这是我的代码:

(主要 Activity )XML布局: :

    <RelativeLayout "
android:id="@+id/texttoto"
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.toto.MainActivity$PlaceholderFragment" >

<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_above="@+id/editText_mail"
android:layout_alignLeft="@+id/editText_mail"
android:onClick="test_trucs"
android:text="@string/test" />

<EditText
android:id="@+id/txttopass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="@string/texttopass" >
</EditText>

</RelativeLayout>

** (第二项 Activity )XML布局: *:*
 <RelativeLayout "
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.toto.Blabla$PlaceholderFragment" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</RelativeLayout>

**** MainActivity.JAVA:****
    package com.toto;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.toto.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}


@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* 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;
}
}
/** mettre les class ICI **/
public void test_trucs(View view) {
Intent intent = new Intent(this, Blabla.class);
EditText editText = (EditText) findViewById(R.id.txttopass);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}

}

**** Blabla.JAVA:****
package com.toto;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Blabla extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blabla);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView1 = new TextView(this);
textView1.setTextSize(40);
textView1.setText(message);

// Set the text view as the activity layout
setContentView(textView1);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.blabla, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* 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_blabla,
container, false);
return rootView;
}
}

}

logcat显示如下:
06-19 15:35:47.982: E/AndroidRuntime(22378): FATAL EXCEPTION: main
06-19 15:35:47.982: E/AndroidRuntime(22378): Process: com.toto, PID: 22378
06-19 15:35:47.982: E/AndroidRuntime(22378): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.toto/com.toto.Blabla}: java.lang.NullPointerException
06-19 15:35:47.982: E/AndroidRuntime(22378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
06-19 15:35:47.982: E/AndroidRuntime(22378): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
06-19 15:35:47.982: E/AndroidRuntime(22378): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-19 15:35:47.982: E/AndroidRuntime(22378): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-19 15:35:47.982: E/AndroidRuntime(22378): at android.os.Handler.dispatchMessage(Handler.java:102)
06-19 15:35:47.982: E/AndroidRuntime(22378): at android.os.Looper.loop(Looper.java:136)
06-19 15:35:47.982: E/AndroidRuntime(22378): at android.app.ActivityThread.main(ActivityThread.java:5001)
06-19 15:35:47.982: E/AndroidRuntime(22378): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 15:35:47.982: E/AndroidRuntime(22378): at java.lang.reflect.Method.invoke(Method.java:515)
06-19 15:35:47.982: E/AndroidRuntime(22378): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-19 15:35:47.982: E/AndroidRuntime(22378): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-19 15:35:47.982: E/AndroidRuntime(22378): at dalvik.system.NativeStart.main(Native Method)
06-19 15:35:47.982: E/AndroidRuntime(22378): Caused by: java.lang.NullPointerException
06-19 15:35:47.982: E/AndroidRuntime(22378): at com.toto.Blabla.onCreate(Blabla.java:39)
06-19 15:35:47.982: E/AndroidRuntime(22378): at android.app.Activity.performCreate(Activity.java:5231)
06-19 15:35:47.982: E/AndroidRuntime(22378): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-19 15:35:47.982: E/AndroidRuntime(22378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
06-19 15:35:47.982: E/AndroidRuntime(22378): ... 11 more

我知道这可能是有史以来最新的事情,但是请放纵..

先谢谢您的帮助 !

最佳答案

您两次调用setContentView,第一个:

setContentView(R.layout.activity_blabla);

不在教程中。我不确定到底是什么引起了NullPointer异常,但是似乎两次调用都会导致问题。

如果您想使用更复杂的布局(R.layout_blabla)并显示消息,就像文本框在该布局中一样,请使用findViewById从onCreate引用它,而不是创建一个新的TextView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blabla);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView1 = (TextView)findViewById(R.id.textView1)
textView1.setTextSize(40);
textView1.setText(message);

// Set the text view as the activity layout
setContentView(textView1);

}

关于android-activity - android Activity 崩溃无缘无故?也许是因为setcontentview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24310585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com