[英]Simple android app with one button: “unfortunately -app- has stopped”
I'm completely new to Android. 我是Android的新手。 I want just a button that does a task but the sample code on the internet does not work.
我只需要一个执行任务的按钮,但是Internet上的示例代码不起作用。 Even though eclipse does not give any errors i can't run this app on my device.
即使eclipse没有给出任何错误,我也无法在设备上运行此应用。 Here's the code:
这是代码:
package com.example.myfirstapp;
import com.example.myfirstapp.R;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
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;
@SuppressLint("ValidFragment")
public class MainActivity extends ActionBarActivity {
static Button btn;
@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 implements View.OnClickListener {
public PlaceholderFragment() {
}
public void onClick(View v){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sicioldrart.com"));
startActivity(browserIntent);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
btn=(Button) getView().findViewById(R.id.bottone);
btn.setOnClickListener(this);
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Thanks for the help 谢谢您的帮助
You need to have an inflated view before you can look for yout button. 您需要先放大视图,然后才能找到按钮。
move this: 移动:
btn=(Button) getView().findViewById(R.id.bottone);
btn.setOnClickListener(this);
below 下面
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
and change getView()
with rootView
和更改
getView()
与rootView
btn=(Button) getView().findViewById(R.id.bottone);
btn.setOnClickListener(this);
these two lines will come after 这两行将
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
and replace getView() with rootView 并用rootView替换getView()
btn=(Button) rootView.findViewById(R.id.bottone);
btn.setOnClickListener(this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.