好的,我整天都在编程,几乎在任何地方都可以搜索,但是什么都没有。希望您能对我有所帮助。我有两个活动,我希望他们进行交流。这是代码
主要活动
public class MainActivity extends Activity{
ImageView imgsettings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgsettings = (ImageView) findViewById(R.id.imgviewsettings);
imgsettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AppSettings.class);
startActivity(intent);
}
});
}
}
显现
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AppSettings"
android:label="@string/title_activity_app_settings" >
</activity>
</application>
AppSettings
package com.example.oldie;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class AppSettings extends Activity implements OnClickListener {
TextView lblkeyword, txtkeyword;
TextView lblnumber, txtnumber;
TextView lbladdress, txtaddress;
Button btnok;
String filename = "preferences.txt";
public static int count = 0;
private static String keyword = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lblkeyword = (TextView) findViewById(R.id.lblkeyword);
txtkeyword = (TextView) findViewById(R.id.txtkeyword);
lblnumber = (TextView) findViewById(R.id.lblnumber);
txtnumber = (TextView) findViewById(R.id.txtnumber);
lbladdress = (TextView) findViewById(R.id.lbladdress);
txtaddress = (TextView) findViewById(R.id.txtaddress);
btnok = (Button) findViewById(R.id.btnok);
btnok.setOnClickListener(this);
lblkeyword.setOnClickListener(this);
lblnumber.setOnClickListener(this);
lbladdress.setOnClickListener(this);
//metritis gia na emfanisei mono mia fora to minima
if(count==0){
Toast.makeText(getApplicationContext(), "Tab on the labels for more information", Toast.LENGTH_LONG).show();
count = count + 1;
}
updateUIFromFiles();
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.lblkeyword:
Toast.makeText(getApplicationContext(), "Set a keyword set that will trigger the SMS checker", Toast.LENGTH_SHORT).show();
break;
case R.id.lblnumber:
Toast.makeText(getApplicationContext(), "Set a number that the app will communicate in case of emmergency", Toast.LENGTH_SHORT).show();
break;
case R.id.lbladdress:
Toast.makeText(getApplicationContext(), "Set the home address\nex. Agias Lavras 19, Piraeus, Greece", Toast.LENGTH_SHORT).show();
break;
case R.id.btnok:
if((txtkeyword.getText().toString().equals("")) || (txtaddress.getText().toString().equals("")) ||(txtnumber.getText().toString().equals(""))){
Toast.makeText(this, "Please fill all the Text Fields.", Toast.LENGTH_SHORT).show();
}
else{
savePreferences();
keyword = txtkeyword.getText().toString();
finish();
}
break;
}
}
private void savePreferences() {
//thewrithike anagkaio na apothikeutoun ta stoixeia se ena arxeio gia na mi xathoun.
try{
OutputStreamWriter outfile = new OutputStreamWriter(openFileOutput(filename, 0));
outfile.write(txtkeyword.getText().toString() + "\n" + txtnumber.getText().toString() + "\n" + txtaddress.getText().toString());
outfile.close();
Toast.makeText(this, "The contents are saved in the file.", Toast.LENGTH_LONG).show();
}
catch(Throwable t) {
Toast.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG).show();
}
}
private void updateUIFromFiles() {
try {
InputStream infile = openFileInput(filename);
if (infile != null){
InputStreamReader tmp = new InputStreamReader(infile);
BufferedReader reader = new BufferedReader(tmp);
txtkeyword.setText(reader.readLine());
txtnumber.setText(reader.readLine());
txtaddress.setText(reader.readLine());
infile.close();
}
}
catch (java.io.FileNotFoundException e) {
// that's OK, we probably haven't created it yet
}
catch (Throwable t) {
Toast.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG).show();
}
}
public String GetMessage(){ //Methodos gia na parei to service to keyword.
return keyword;
}
}
问题是
06-19 22:17:03.789: E/AndroidRuntime(17204): FATAL EXCEPTION: main
06-19 22:17:03.789: E/AndroidRuntime(17204): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.oldie/com.example.oldie.AppSettings}: java.lang.NullPointerException
06-19 22:17:03.789: E/AndroidRuntime(17204): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
06-19 22:17:03.789: E/AndroidRuntime(17204): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
06-19 22:17:03.789: E/AndroidRuntime(17204): at android.app.ActivityThread.access$600(ActivityThread.java:127)
06-19 22:17:03.789: E/AndroidRuntime(17204): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
06-19 22:17:03.789: E/AndroidRuntime(17204): at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 22:17:03.789: E/AndroidRuntime(17204): at android.os.Looper.loop(Looper.java:137)
06-19 22:17:03.789: E/AndroidRuntime(17204): at android.app.ActivityThread.main(ActivityThread.java:4441)
06-19 22:17:03.789: E/AndroidRuntime(17204): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 22:17:03.789: E/AndroidRuntime(17204): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 22:17:03.789: E/AndroidRuntime(17204): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-19 22:17:03.789: E/AndroidRuntime(17204): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-19 22:17:03.789: E/AndroidRuntime(17204): at dalvik.system.NativeStart.main(Native Method)
06-19 22:17:03.789: E/AndroidRuntime(17204): Caused by: java.lang.NullPointerException
06-19 22:17:03.789: E/AndroidRuntime(17204): at com.example.oldie.AppSettings.onCreate(AppSettings.java:39)
06-19 22:17:03.789: E/AndroidRuntime(17204): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-19 22:17:03.789: E/AndroidRuntime(17204): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
06-19 22:17:03.789: E/AndroidRuntime(17204): ... 11 more
Intent的代码根本不起作用。我的应用程序崩溃并结束。在服用药来控制自己的愤怒之前,我迈出了一步。我不知道出了什么问题,意图也没有用。请帮我。
在AppSettings活动中,您将内容视图设置为R.layout.activity_main。我猜那不是您的意图。它可能没有您期望找到的TextViews。查找它们的调用返回null,这将在您调用setOnClickListener()时导致异常。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句