Java 我能';我的钮扣坏了 我只是想开始在Android上做APS,我知道C++有点,但是我用最简单的东西打了几天,我不能让我的按钮工作。
我尝试了每一种教程方法,但我不能让监听器按按钮——每次我这么做,我的程序都会崩溃 代码很简单(我不会更改我的方法下面的任何内容),我还启动了全新的Android项目,只做了所有教程中的按钮和文本视图编辑:Java 我能';我的钮扣坏了 我只是想开始在Android上做APS,我知道C++有点,但是我用最简单的东西打了几天,我不能让我的按钮工作。,java,android,button,onclick,listener,Java,Android,Button,Onclick,Listener,我尝试了每一种教程方法,但我不能让监听器按按钮——每次我这么做,我的程序都会崩溃 代码很简单(我不会更改我的方法下面的任何内容),我还启动了全新的Android项目,只做了所有教程中的按钮和文本视图编辑: package com.example.buttonwork; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBarActivity;
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;
}
}
}
每次程序崩溃。我所有的东西都导入了,ID和名字也很好。请帮助我,因为我现在绝望了。我只是想完成这些简单的事情,开始编写一些真正能够工作、具有和输出的代码,但我被困在这样一个简单的步骤中…使用这个方法,并从
onCreate(Bundle savedInstanceState)
方法中删除tb1
和bt1
初始化代码
/**
* 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);
tv1 = (TextView) (rootView.findViewById(R.id.tv1));
but1 = (Button) (rootView.findViewById(R.id.b1));
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv1.setText("Button pressed");
}
});
return rootView;
}
}
给你。。这就是你犯错误的地方。您试图在活动中获取引用,而不是已定义的片段 从您的onCreate()中删除按钮,并在您的占位符片段中添加这些组件。这就是您获得空指针的原因 注意:使用getActivity()引用片段中的组件 在你的碎片中,你必须做出这样的改变:
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");
}
});
}
}
希望这有帮助:) 天哪!你的按钮怎么了?例外?日志中的堆栈跟踪将准确地告诉我们问题所在。通过编辑你的问题来发布,然后了解别人是如何解决问题的。当然,调试器是实现这一点的最简单方法。但是3可能是空的。是否在activity_main.xml中?请发布xml。您定义的按钮是在您的片段中,还是在您的片段中,还是在您的主布局中??因为如果它在片段中,您必须从片段活动而不是从主活动中更改文本。这就是空指针的原因。它非常有用!非常感谢你,伙计!I’我很高兴能帮上忙。。请接受未来观众的答案:)
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");
}
});
}
}