[英]TextView causing app to crash
For some reason when I call the TextView function it crashes my app. 由于某种原因,当我调用TextView函数时,它会使我的应用程序崩溃。 I have looked through the answers of questions similar to mine but I could not narrow down my problem.
我已经浏览了与我类似的问题的答案,但无法缩小问题的范围。
This is the first time I am making an app, first time I am using Java for that matter. 这是我第一次制作应用程序,这是我第一次使用Java。 Previously I only have experience with C++/C.
以前,我只有C ++ / C经验。
From my Android Manisfest File 从我的Android Manisfest文件
<activity
android:name="sg.blogspot.ce9005project.LegalNoticesActivity"
android:label="@string/action_legalnotices" >
</activity>
From the .xml file: 从.xml文件中:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textId"/>
My java file: 我的Java文件:
package sg.blogspot.ce9005project;
import com.google.android.gms.common.GooglePlayServicesUtil;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
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.TextView;
import android.widget.Toast;
import android.os.Build;
public class LegalNoticesActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_legal_notices);
//Original line below
((TextView)
findViewById(R.id.textId)).setText(GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(this));
//The 2 lines below were modified by me but still do not work
//TextView textView = (TextView) findViewById(R.id.textId);
//textView.setText("TEST");
//The below line works. The problem, that means, lies with TextView
//Toast.makeText(this, GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(this),
Toast.LENGTH_SHORT).show();
}
}
findViewById(R.id.textId)).setText(GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(this));
is wrong. 是错的。
You didn't DECLARE a TextView before using it . 在使用TextView之前,您没有对它进行过声明 。
Wild guess: you have a NullPointerException, right? 疯狂猜测:您有NullPointerException,对吗?
Make sure you have a TextView with id textId
in /res/layout/activity_legal_notices.xml
确保在
/res/layout/activity_legal_notices.xml
具有ID为textId
的TextView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.