我的目标是编制一个反应用程序。智能手机中间有一张照片,照片下方是从100开始的倒计时。
每次点击图片时,图片都会闪烁,计数器应该减少,直到你点击100次并达到0。
我有一些代码,但每次在我的VM上运行项目时,应用程序都会崩溃。
package com.syntaix.appleclicker;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
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.ImageView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
int counter = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = (TextView) findViewById(R.id.TextView01);
final ImageView image = (ImageView) findViewById(R.id.appel);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter--;
text.setText("" + counter);
}
});
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;
}
}
}
这是LogCat截图: LogCat
答案 0 :(得分:0)
您可以在创建片段时找到片段中的视图。但是在将片段附加到活动之前,您正试图获取视图。您需要将一些代码移动到片段,如下所示......
MainActivity.java
@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();
}
}
PlaceholderFragment.java
public static class PlaceholderFragment extends Fragment {
int counter = 100;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
final TextView text = (TextView) rootView.findViewById(R.id.TextView01);
final ImageView image = (ImageView) rootView.findViewById(R.id.appel);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter--;
text.setText("" + counter);
}
});
return rootView;
}
}