gpt4 book ai didi

java - 我无法让我的按钮工作

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

我只是想开始在 Android 上制作 aps,我懂一点 C++,但我为最简单的事情奋斗了几天 - 我无法让我的按钮工作。

我尝试了所有教程方法,但我无法让听众按下按钮 - 每次我这样做时,我的程序都会崩溃。

代码很简单(我没有更改我的方法下面的任何内容),我还启动了全新的 Android 项目,只是像所有教程中那样进行了这些按钮和 TextView 编辑:

package com.example.buttonwork;

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.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
Button but1;
TextView tv1;
@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();
}
tv1 = (TextView) findViewById(R.id.tv1);
but1 = (Button) findViewById(R.id.b1);
but1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv1.setText("Button pressed");
}
});
}


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

}

The LOGCAT:
04-26 15:51:39.435: W/dalvikvm(7185): threadid=1: thread exiting with uncaught exception (group=0x418762a0)
04-26 15:51:39.450: E/AndroidRuntime(7185): FATAL EXCEPTION: main
04-26 15:51:39.450: E/AndroidRuntime(7185): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttonwork/com.example.buttonwork.MainActivity}: java.lang.NullPointerException
04-26 15:51:39.450: E/AndroidRuntime(7185): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
04-26 15:51:39.450: E/AndroidRuntime(7185): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
04-26 15:51:39.450: E/AndroidRuntime(7185): at android.app.ActivityThread.access$700(ActivityThread.java:140)
04-26 15:51:39.450: E/AndroidRuntime(7185): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
04-26 15:51:39.450: E/AndroidRuntime(7185): at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 15:51:39.450: E/AndroidRuntime(7185): at android.os.Looper.loop(Looper.java:137)
04-26 15:51:39.450: E/AndroidRuntime(7185): at android.app.ActivityThread.main(ActivityThread.java:4921)
04-26 15:51:39.450: E/AndroidRuntime(7185): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 15:51:39.450: E/AndroidRuntime(7185): at java.lang.reflect.Method.invoke(Method.java:511)
04-26 15:51:39.450: E/AndroidRuntime(7185): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
04-26 15:51:39.450: E/AndroidRuntime(7185): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
04-26 15:51:39.450: E/AndroidRuntime(7185): at dalvik.system.NativeStart.main(Native Method)
04-26 15:51:39.450: E/AndroidRuntime(7185): Caused by: java.lang.NullPointerException
04-26 15:51:39.450: E/AndroidRuntime(7185): at com.example.buttonwork.MainActivity.onCreate(MainActivity.java:29)
04-26 15:51:39.450: E/AndroidRuntime(7185): at android.app.Activity.performCreate(Activity.java:5206)
04-26 15:51:39.450: E/AndroidRuntime(7185): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-26 15:51:39.450: E/AndroidRuntime(7185): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
04-26 15:51:39.450: E/AndroidRuntime(7185): ... 11 more

而且程序每次都会崩溃。我已经导入了所有内容,ID 和名称也很好。请帮助我,因为现在我很绝望。我只是想完成这些简单的事情并开始编写一些真正有效、具有和输出的东西,但我陷入了如此简单的步骤......

最佳答案

给你..这就是你犯错误的地方。您试图获取 Activity 中的引用而不是您定义的 fragment 。

onCreate() 中删除按钮,并在占位符 fragment 中添加这些组件。这就是您得到空指针。

注意:使用getActivity()来引用 fragment 中的组件。

在您的 fragment 中,您必须进行如下更改:

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(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);

Button but1 = (Button)getActivity(). findViewById(R.id.b1);
TextView tv1 = (TextView)getActivity(). findViewById(R.id.tv1);

but1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

tv1.setText("Button pressed");

}
});
}
}

希望这有帮助..:)

关于java - 我无法让我的按钮工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23311356/

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