Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在文本视图中添加OnClick(视图v)_Java_Android - Fatal编程技术网

Java 在文本视图中添加OnClick(视图v)

Java 在文本视图中添加OnClick(视图v),java,android,Java,Android,我在我的文本视图的主活动中添加了onClick,我没有错误,但当我运行应用程序时,它崩溃了,说应用程序停止工作,即使我的代码中没有错误,我也不知道问题出在哪里 我希望你能帮助我,并提前表示感谢 这是我的日志 12-25 03:32:44.694: I/Ads(1433): AdLoader timed out after 60000ms while getting the URL. 12-25 03:33:00.234: I/Process(1433): Sending signal. PID:

我在我的文本视图的主活动中添加了onClick,我没有错误,但当我运行应用程序时,它崩溃了,说应用程序停止工作,即使我的代码中没有错误,我也不知道问题出在哪里

我希望你能帮助我,并提前表示感谢

这是我的日志

12-25 03:32:44.694: I/Ads(1433): AdLoader timed out after 60000ms while getting the URL.
12-25 03:33:00.234: I/Process(1433): Sending signal. PID: 1433 SIG: 9
12-25 03:33:38.454: D/dalvikvm(1559): GC_FOR_ALLOC freed 40K, 4% free 3135K/3248K, paused 283ms, total 286ms
12-25 03:33:38.504: I/dalvikvm-heap(1559): Grow heap (frag case) to 4.200MB for 1127536-byte allocation
12-25 03:33:38.654: D/dalvikvm(1559): GC_FOR_ALLOC freed 2K, 3% free 4233K/4352K, paused 146ms, total 146ms
12-25 03:33:39.144: D/dalvikvm(1559): GC_FOR_ALLOC freed <1K, 3% free 4238K/4352K, paused 46ms, total 47ms
12-25 03:33:39.304: I/dalvikvm-heap(1559): Grow heap (frag case) to 9.535MB for 5591056-byte allocation
12-25 03:33:39.394: D/dalvikvm(1559): GC_FOR_ALLOC freed <1K, 2% free 9698K/9816K, paused 84ms, total 84ms
12-25 03:33:40.284: V/WebViewChromium(1559): Binding Chromium to the background looper Looper (main, tid 1) {b1d56e50}
12-25 03:33:40.304: I/chromium(1559): [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
12-25 03:33:40.334: I/BrowserProcessMain(1559): Initializing chromium process, renderers=0
12-25 03:33:40.514: D/(1559): HostConnection::get() New Host Connection established 0xb7a714f8, tid 1559
12-25 03:33:40.594: W/chromium(1559): [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
12-25 03:33:41.664: E/ActivityThread(1559): Failed to find provider info for com.google.plus.platform
12-25 03:33:41.774: I/Ads(1559): To get test ads on this device, call adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
12-25 03:33:42.194: D/AndroidRuntime(1559): Shutting down VM
12-25 03:33:42.194: W/dalvikvm(1559): threadid=1: thread exiting with uncaught exception (group=0xb1a83ba8)
12-25 03:33:42.214: E/AndroidRuntime(1559): FATAL EXCEPTION: main
12-25 03:33:42.214: E/AndroidRuntime(1559): Process: imamalsajadsayings.android.com, PID: 1559
12-25 03:33:42.214: E/AndroidRuntime(1559): java.lang.RuntimeException: Unable to start activity ComponentInfo{imamalsajadsayings.android.com/imamalsajadsayings.android.com.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.LinearLayout
12-25 03:33:42.214: E/AndroidRuntime(1559):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-25 03:33:42.214: E/AndroidRuntime(1559):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-25 03:33:42.214: E/AndroidRuntime(1559):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-25 03:33:42.214: E/AndroidRuntime(1559):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-25 03:33:42.214: E/AndroidRuntime(1559):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-25 03:33:42.214: E/AndroidRuntime(1559):     at android.os.Looper.loop(Looper.java:136)
12-25 03:33:42.214: E/AndroidRuntime(1559):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-25 03:33:42.214: E/AndroidRuntime(1559):     at java.lang.reflect.Method.invokeNative(Native Method)
12-25 03:33:42.214: E/AndroidRuntime(1559):     at java.lang.reflect.Method.invoke(Method.java:515)
12-25 03:33:42.214: E/AndroidRuntime(1559):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-25 03:33:42.214: E/AndroidRuntime(1559):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-25 03:33:42.214: E/AndroidRuntime(1559):     at dalvik.system.NativeStart.main(Native Method)
12-25 03:33:42.214: E/AndroidRuntime(1559): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.LinearLayout
12-25 03:33:42.214: E/AndroidRuntime(1559):     at imamalsajadsayings.android.com.MainActivity.onCreate(MainActivity.java:20)
12-25 03:33:42.214: E/AndroidRuntime(1559):     at android.app.Activity.performCreate(Activity.java:5231)
12-25 03:33:42.214: E/AndroidRuntime(1559):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-25 03:33:42.214: E/AndroidRuntime(1559):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-25 03:33:42.214: E/AndroidRuntime(1559):     ... 11 more
12-25 03:33:43.064: D/dalvikvm(1559): DexOpt: --- BEGIN 'ads478949099.jar' (bootstrap=0) ---
12-25 03:33:46.024: D/dalvikvm(1559): DexOpt: --- END 'ads478949099.jar' (success) ---
12-25 03:33:46.024: D/dalvikvm(1559): DEX prep '/data/data/imamalsajadsayings.android.com/cache/ads478949099.jar': unzip in 54ms, rewrite 2963ms
12-25 03:33:46.844: I/Ads(1559): adRequestUrlHtml: <html><head><script src="http://media.admob.com/sdk-core-v40.js"></script><script>AFMA_getSdkConstants();AFMA_buildAdURL({"preqs":0,"session_id":"5927457787414134416","seq_num":"1","slotname":"a152b5665edfefd","u_w":384,"msid":"imamalsajadsayings.android.com","cap":"m,a","js":"afma-sdk-a-v6.4.1","bas_off":0,"net":"ed","app_name":"3.android.imamalsajadsayings.android.com","hl":"en","gnt":3,"carrier":"310260","u_audio":4,"kw":[],"u_sd":2,"simulator":1,"ms":"OPbcxzOd9lJImWjXOtp3hqpv26-cVzCC2CRXlOnJL4iR9JnqERa-t0gYOUEDfm9_ZdloVn6JYXvSi2W_T-OmyZGApgUSdoc5Ymf5xM0wqWv4Q1-L-cPGOlLqFb__qZoBPosFpAT3wG8rSE4SjadFbESExgSB07bJ9U3TVzf2AlhGPQR-7N41d3arIEUnV2UCOeer6Y6n_imlZs00ubvpKIcI5sAN2ZBIIoZhg4Qi35EBNf6nHyiKHVecGbgmnf6PDDO1xU1MfLOSdMQeqKZQWyiIsXWPGCBZ6wqyhJmCfeABwzF4p0G6y_gKB2r9xs_HzB4e8lrIQm_l1chnFG96Tg","isu":"B3EEABB8EE11C2BE770B684D95219ECB","format":"320x50_mb","oar":0,"ad_pos":{"height":0,"visible":0,"y":0,"x":0,"width":0},"u_h":592,"pt":1,"bas_on":0,"ptime":0});</script></head><body></body></html>
12-25 03:33:48.574: I/Process(1559): Sending signal. PID: 1559 SIG: 9
这是我的activity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right"
    android:orientation="vertical"
    tools:context=".MainActivity" >

                        <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="right"
                        android:orientation="vertical" 
                        android:background="@drawable/images" >



                       <com.google.ads.AdView 
                       android:id="@+id/ad"
                       android:layout_width="wrap_content"  
                       android:layout_height="wrap_content"     
                       ads:adUnitId="a152b5665edfefd"
                       ads:loadAdOnCreate="true"
                       ads:adSize="BANNER" />




                    <TextView
                        android:id="@+id/state1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="قال (عليه السلام): (من كرمت عليه نفسه هانت عليه الدنيا)                                                                         "
                        android:paddingTop="10sp"
                        android:clickable = "false"
                        style="?android:listSeparatorTextViewStyle" />




                    <TextView
                        android:id="@+id/state2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="قال (عليه السلام): (الخير كله في صيانة الإنسان نفسه)                                                                "
                        android:paddingTop="10sp"             
                        style="?android:listSeparatorTextViewStyle"
                        android:clickable="true" 
                        android:onClick="runNextTask"/>

                    <TextView
                        android:id="@+id/state3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="قال (عليه السلام): (ما أحب المؤمن معافى في الدنيا، وفي نفسه وماله، ولا يصاب بشيء من المصائب)"
                        android:paddingTop="10sp"
                        android:clickable = "true"
                        style="?android:listSeparatorTextViewStyle"/>

                    <TextView
                        android:id="@+id/state4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=" قال (عليه السلام): (ضل من ليس له حليم يرشده، وذل من ليس له سفيه يعضده..)                                                "
                        android:paddingTop="10sp"
                        android:clickable = "true"
                        style="?android:listSeparatorTextViewStyle"/>

                    <TextView
                        android:id="@+id/state5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="قال (عليه السلام): (ويل لمن غلبت آحاده أعشاره)                                                                                         "
                        android:paddingTop="10sp"
                        android:clickable = "true"
                        style="?android:listSeparatorTextViewStyle"/>

                    <TextView
                        android:id="@+id/state6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="قال (عليه السلام): (اللئيم يأكل ماله الأعداء، والذي خبث لا يخرج إلا نكداً..)                                                 "
                        android:paddingTop="10sp"
                        android:clickable = "true"
                        style="?android:listSeparatorTextViewStyle"/>

                    <View
                        android:id="@+id/view3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:id="@+id/state7"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="       قال (عليه السلام): (إن الجسد إذا لم يمرض يأشر ولا خير في جسد يأشر..)                                                                       "
                        android:paddingTop="10sp"
                        android:clickable = "true"
                        style="?android:listSeparatorTextViewStyle"/>

                    <TextView
                        android:id="@+id/state8"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="قال (عليه السلام): (عليكم بأداء الأمانة، فوالذي بعث محمداً بالحق نبياً لو أن قاتل أبي الحسين بن علي ائتمنني على السيف الذي قتله به لأديته إليه..)"
                        android:paddingTop="10sp"
                        android:clickable = "true"
                        style="?android:listSeparatorTextViewStyle"/>

                    <TextView
                        android:id="@+id/state9"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="قال (عليه السلام): (أفضل الأعمال عند الله ما عمل بالسنة)                                                                   "
                        android:paddingTop="10sp"
                        android:clickable = "true"
                        style="?android:listSeparatorTextViewStyle"/>

                    <TextView
                        android:id="@+id/state9"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="قال (عليه السلام): (لقد استرقك بالود من سبقك بالشكر..)                                                                              "
                        android:paddingTop="10sp"
                        android:clickable = "true"
                        style="?android:listSeparatorTextViewStyle"/>

                    <TextView
                        android:id="@+id/state10"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="قال (عليه السلام): (لا يكون الصديق صديقاً حتى يقطع لأخيه المؤمن قطعة من دينه يرقعها بالاستغفار..)"
                        android:paddingTop="10sp"
                        android:clickable = "true"
                        style="?android:listSeparatorTextViewStyle"/>

                    <TextView
                        android:id="@+id/state11"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="قال (عليه السلام): (ضمنت على ربي أنه لا يسال أحد من غير حاجة إلا اضطرته المسألة يوماً إلى أن يسأل من حاجة..)"
                        android:paddingTop="10sp"
                        android:clickable = "true"
                        style="?android:listSeparatorTextViewStyle"/>

                    <TextView
                        android:id="@+id/state12"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="قال (عليه السلام): (إياك، وما تعتذر منه)                                                                                                                  "
                        android:paddingTop="10sp"
                        android:clickable = "true"
                        style="?android:listSeparatorTextViewStyle"/>

                    <TextView
                        android:id="@+id/state13"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="قال (عليه السلام): (إن الله جل جلاله يقول: وعزتي وعظمتي وجمالي، وبهائي، وعلوي، وارتفاع مكان لا يؤثر عبد هواي على هواه إلا جعلت همه في آخرته، وغناه في قلبه، كففت عنه ضيعته، وضمنت السماوات والأرض رزقه، وأتته الدنيا وهي راغمة)"
                        android:paddingTop="10sp"
                        android:clickable = "true"
                        style="?android:listSeparatorTextViewStyle"/>

                     <TextView
                         android:id="@+id/state14"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="قال (عليه السلام): (رب مغرور، مفتون، يصبح لاهياً ضاحكاً، يأكل ويشرب، وهو لا يدري لعله قد سبقت له من الله سخطه، يصلى بها نار جهنم..)"
                        android:paddingTop="10sp"
                        android:clickable = "true"
                        style="?android:listSeparatorTextViewStyle"/>



        </LinearLayout>

</ScrollView>

您在布局中添加了
TextView
时,错误地初始化了
TextView
,您试图使用
LinearLayout
对其进行初始化,这是错误的

在onCreate()中使用
TextView
更改
LinearLayout
,如下所示:

 someLayout = (TextView) findViewById(R.id.state2); 
为什么在代码中要将布局膨胀两次?我不明白你为什么这样做。但是我提供了一些相关的代码,请尝试使用我的代码

public class MainActivity extends Activity {
private LayoutInflater inflater;
private TextView someLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       someLayout = (TextView) findViewById(R.id.state2); //layout present in      activity_main
      // inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
     runNextTask();

 }
 public void runNextTask(){
       //  LinearLayout mInflatedLayout = (LinearLayout)               inflater.inflate(R.layout.activity_main, null);
      @SuppressWarnings("unused")
    final TrackerInfo newInfo = new TrackerInfo();
    //set up for model selection
   // TextView modelTextview = (TextView)mInflatedLayout.findViewById(R.id.state2); 
    //someLayout.addView(mInflatedLayout);                   
    someLayout.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {

       }
    });
 }
仅供参考,您不能在
文本视图
中添加任何版面,因为其
视图
不是
版面
,在您的代码中,我看到您试图将
版面
添加到
视图
中,这是错误的

更改为

public void runNextTask(View v){ // method signature is worng
如果textView位于activity_main.xml中,也是如此。没有必要对布局进行充气

setContentView(R.layout.activity_main); // is enough
并在onCreate中初始化textView

TextView modelTextview = (TextView)findViewById(R.id.state2); 
如果您想让单击listener以渐进方式删除

android:onClick="runNextTask"

线性布局和文本视图的id都相同,如果是这样,请更改线性布局的id。通过下面的代码行可以清楚地看到,您已经为线性布局和文本视图指定了相同的id

someLayout = (LinearLayout) findViewById(R.id.state2);

这里的问题是
someLayout=(LinearLayout)findViewById(R.id.state2)

您正在尝试将textview强制转换为linearlayout


使用
yourtextview=(TextView)findViewById(R.id.state2)

发布您的代码也是因为它清楚地表明java.lang.ClassCastException:android.widget.TextView不能强制转换为android.widget.linearLayout您的代码非常混乱。您确定自己是android开发人员。同时从TextView xml中删除
android:onClick=“runNextTask”
属性,因为您正在codehere
someLayout=(LinearLayout)findViewById(R.id.state2)中添加click listener要将TextView强制转换到的对象LinearLayout@user3115742请查看我的答案并尝试使用该代码。我对你的代码做了一些相关的修改。
someLayout = (LinearLayout) findViewById(R.id.state2);